//
//  DestinationViewController.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/7.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "DestinationViewController.h"
#import "TitleTableViewCell.h"
#import "VCbannerModel.h"
#import "DestimationTableViewCell.h"
#import "MoreViewController.h"
#import "DetailedFirstViewController.h"

@interface DestinationViewController ()<UITableViewDataSource, UITableViewDelegate, WebDetegate>

@property (nonatomic,strong) DestinationModel *destinationModel;
//轮播图数组
@property (nonatomic, strong) NSMutableArray *focusimagearr;
//每日精选故事数组
@property (nonatomic, strong) NSMutableArray *gooddaystoryarr;
//标题数组
@property (nonatomic, strong) NSMutableArray *titlearray;
//webcell数组
@property (nonatomic, strong) NSMutableArray *webarr;
//自定义cell数组
@property (nonatomic, strong) NSMutableArray *cunstomarr;
//Uitablecell
@property (nonatomic, strong) UITableView *tableview;

@end

@implementation DestinationViewController


- (void)viewDidLoad {
    [self getDataSource];
    [self createTableview];
    
    
}

- (void)getDataSource {
    [XYNetWorkTool getWithUrl:DestinationURL parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        self.destinationModel = [[DestinationModel alloc] initWithDataSource:result];
        [self.tableview reloadData];
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self addfocusimageheaderview];
        });
    
    } fail:^(NSError *error) {
        NSLog(@"目的地的首页数据获取失败，错误为%@", error);
    }];
}

/**
 *  创建tableview
 */
- (void)createTableview {
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT - 44 * XHIGHT) style:UITableViewStylePlain];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.bounces = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];

    
    [self.tableview registerClass:[DestimationTableViewCell class] forCellReuseIdentifier:@"DestimationTableViewCell"];
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
}

/**
 *  tableview协议方法
 */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.destinationModel.elementArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 400*XHIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    DestimationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DestimationTableViewCell"];
    cell.elementModel = self.destinationModel.elementArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.pushMoreController = ^(MoreModel *moreModel) {
        MoreViewController *moreVC = [[MoreViewController alloc] init];
        moreVC.moreModel = moreModel;
        [self.navigationController pushViewController:moreVC animated:YES];
    };
    cell.pushDetailedVC = ^(VCsearchElementModel *elementModel) {
        DetailedFirstViewController *detailedVC = [[DetailedFirstViewController alloc] init];
        detailedVC.elementModel = elementModel;
        [self.navigationController pushViewController:detailedVC animated:YES];
    };
        return cell;
    
    
}





/**
 *  轮播图
 */
- (void)addfocusimageheaderview {
    NSMutableArray *focusimagearray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *focusurlarray = [[NSMutableArray alloc] initWithCapacity:0];
    for (VCbannerModel *banner in self.destinationModel.bannerArray) {
        [focusimagearray addObject:banner.image_url];
        [focusurlarray addObject:banner.html_url];
    }
    ScrollWithImage *focusimageviews = [[ScrollWithImage alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 220) withArray:focusimagearray witnArrayurl:focusurlarray withArrayId:nil];
    focusimageviews.detegate = self;
    self.tableview.tableHeaderView = focusimageviews;
}
/**
 *  轮播图点进去
 */
- (void)getWeb:(NSString *)myurl {
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.url = myurl;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
